# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from datetime import datetime, date, timedelta, time
from lxml import etree
from openerp.osv import fields, osv
from openerp.tools.translate import _

class project_create_from_template(osv.osv_memory):
    _name = 'project.create.from.template'

    _columns = {
        'new_date_start' : fields.date('New Project Start Date', select=True, required=True, help="Put here the new project's start date."),
    }

    _defaults = {
        'new_date_start': datetime.strftime(datetime.now(), "%Y-%m-%d")
    }

    def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
        res = super(project_create_from_template, self).fields_view_get(cr, uid, view_id, view_type, context, toolbar, submenu=submenu)
        users_pool = self.pool.get('res.users')
        time_mode = users_pool.browse(cr, uid, uid, context).company_id.project_time_mode_id
        time_mode_name = time_mode and time_mode.name or 'Hours'
        if time_mode_name in ['Hours','Hour']:
            return res

        eview = etree.fromstring(res['arch'])

        def _check_rec(eview):
            if eview.attrib.get('widget','') == 'float_time':
                eview.set('widget','float')
            for child in eview:
                _check_rec(child)
            return True

        _check_rec(eview)

        res['arch'] = etree.tostring(eview)

        for field in res['fields']:
            if 'Hours' in res['fields'][field]['string']:
                res['fields'][field]['string'] = res['fields'][field]['string'].replace('Hours',time_mode_name)
        return res

    def create_project(self, cr, uid, ids, context=None):
        if context is None:
            context = {'type': 'ir.actions.act_window_close'}
        result = {}
        data = self.browse(cr, uid, ids, context=context)[0]
        project_id = context.get('active_id')
        if project_id:
            project_pool = self.pool.get('project.project')
            context.update({'new_date_start':data.new_date_start})
            result = project_pool.duplicate_template(cr, uid, [project_id], context=context)
        return result
    
project_create_from_template()